package JDBCUtil;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.sql.*;
import java.util.Map;

public class JDBCUtil {
    private Connection conn;
    private PreparedStatement ps;

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public JDBCUtil() {
    }

    //------------------通过全局作用域对象得到Connection----------------------start
    public Connection getConnection(HttpServletRequest request){
        // 1.通过请求对象，得到全局作用域对象
        ServletContext application = request.getServletContext();
        // 2.从全局作用域对象得到map
        Map map = (Map)application.getAttribute("key1");
        // 3.从map得到一个处于空闲状态Connection（拖鞋）
        for (Object con : map.keySet()){
            if ((boolean)map.get(con)){
                conn = (Connection) con;
                map.put(con, false);
                break;
            }
        }
        return conn;
    }

    public PreparedStatement getPreparedStatement(String sql, HttpServletRequest request){
        getConnection(request);
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return ps;
    }

    public void close(HttpServletRequest request){
        if (ps != null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        ServletContext application = request.getServletContext();
        Map map = (Map) application.getAttribute("key1");
        map.put(conn, true);
    }

    public void close(ResultSet rs, HttpServletRequest request){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (ps != null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        ServletContext application = request.getServletContext();
        Map map = (Map) application.getAttribute("key1");
        map.put(conn, true);
    }

    public Connection getConnection(){
        try {
            conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/bjpowernode",
                    "root", "nengnengabcd");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return conn;
    }

    public PreparedStatement getPreparedStatement(String sql){
        getConnection();
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return ps;
    }

    public void close(){
        if (ps != null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(ResultSet rs){
        if (rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (ps != null){
            try {
                ps.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        if (conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
